package uit.com.project.iCampus.dao.hibernate;

import java.util.List;

import uit.com.project.iCampus.dao.UserDAO;
import uit.com.project.iCampus.model.User;

public class UserDAOHibernate extends GenericHibernateDAO<User, String> implements UserDAO{

	@SuppressWarnings("unchecked")
	@Override
	public User checkLogin(String username, String password) {
		List<User> list = getSession()
		.createQuery(
				"FROM User AS User WHERE User.username = :username AND User.password = :password")
		.setString("username", username)
		.setString("password", password)
		.list();
		
		if (list != null && list.size() > 0)
			return list.get(0);
		else 
			return null;
	}
/*	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByIdUserLike(String idUser) {
		return getSession()
		.createQuery(
				"FROM User AS User WHERE User.idUser LIKE :pname")
		.setString("pname", "%" + idUser + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByTenUserLike(String tenUser) {
		return getSession()
		.createQuery(
				"FROM User AS User WHERE User.hoTen LIKE :pname")
		.setString("pname", "%" + tenUser + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByIdNhomUserLike(String idNhom) {
		return getSession()
		.createQuery(
				"FROM User AS User WHERE User.nhomUser.idNhomUser LIKE :pname")
		.setString("pname", "%" + idNhom + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findByEmailUserLike(String email) {
		return getSession()
		.createQuery(
				"FROM User AS User WHERE User.email LIKE :pname")
		.setString("pname", "%" + email + "%").list();
	}
	
	public List<User> findByOneCondition(String criteria,
			String keyWord) {
		List<User> result = new ArrayList<User>();
		if (criteria.equals("idUser")) {
			result = findByIdUserLike(keyWord);
		} else if (criteria.equals("tenUser")){
			result = findByTenUserLike(keyWord);
		} else if (criteria.equals("idNhomUser")) {
			result = findByIdNhomUserLike(keyWord);
		} else if (criteria.equals("email")){
			result = findByEmailUserLike(keyWord);
		}
		return result;
	}
*/
	@Override
	public User findByEmail(String email) {
		User result = (User) getSession()
		.createQuery(
				"FROM User AS User WHERE User.email = :email")
		.setString("email", email)
		.uniqueResult();
		
		return result;
	}

}
